﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TerrainGenerator
{
    class BinaryTriangle
    {
        public BinaryTriangle LeftChild { get; set; }
        public BinaryTriangle RightChild { get; set; }

        public BinaryTriangle LeftNeighbor { get; set; }
        public BinaryTriangle RightNeighbor { get; set; }

        public BinaryTriangle BaseNeighbor { get; set; }
        public Triangle Triangle { get { return triangle; } }

        public VarianceTree Tree { get; set; }

        Triangle triangle;

        public BinaryTriangle(Triangle triangle)
        {
            this.triangle = triangle;
            LeftChild = RightChild = LeftNeighbor = RightNeighbor = BaseNeighbor = null;
        }

        public int Index()
        {
            return Triangle.Index;
        }

        public bool IsLeaf()
        {
            return Triangle.IsLeaf();
        }

        public bool IsSplit()
        {
            return LeftChild != null;
        }

        public BinaryTriangle GenLeftChild()
        {
            BinaryTriangle leftChild = new BinaryTriangle(Triangle.LeftChild());
            leftChild.Tree = Tree;
            return leftChild;
        }

        public BinaryTriangle GenRightChild()
        {
            BinaryTriangle rightChild = new BinaryTriangle(Triangle.RightChild());
            rightChild.Tree = Tree;
            return rightChild;
        }

        public float Variance()
        {
            return Tree.Tree[triangle.Index];
        }

        public int CountTriangles()
        {
            if (IsSplit())
            {
                return LeftChild.CountTriangles() + RightChild.CountTriangles();
            }
            else
            {
                return 1;
            }
        }
    }
}
